package com.tianqingwl.gsystem.resources.controller;


import cn.hutool.core.codec.Base64Decoder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.common.vo.CommonResultVo;
import com.tianqingwl.gsystem.common.vo.FileUploadResultVo;
import com.tianqingwl.gsystem.common.vo.FileResourcesResultVo;
import com.tianqingwl.gsystem.resources.pojo.SysConfig;
import com.tianqingwl.gsystem.resources.pojo.SysFileResource;
import com.tianqingwl.gsystem.resources.service.SysConfigService;
import com.tianqingwl.gsystem.resources.service.SysFileResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Controller
@RequestMapping("/manage/sys_file_resource/")
@PreAuthorize("@gs.check('content_manage')")
public class SysFileResourceController {

    @Autowired
    private SysFileResourceService sysFileResourceService;
    @Autowired
    private SysConfigService sysConfigService;

    @RequestMapping("/upload")
    @ResponseBody
    public Object upload(MultipartFile[] files, String action, String scrawlImgStr, Long start) {
        try {
            //网站配置读取
            SysConfig domainConfig = sysConfigService.getSysConfig("site_config", "domain");
            String domain = domainConfig.getConfigValue();

            if (!StringUtils.isEmpty(action) && "config".equals(action)) {
                //TODO 文件上传存储方式可自定义
                Map config = new HashMap();
                //config.put("imageUrl","http://127.0.0.1:8081/manage/sys_file_resource/upload?action=upload");

                //图片上传配置
                config.put("imageActionName", "upload");
                config.put("imageUrlPrefix", "http://" + domain + "/upload/");
                config.put("imageFieldName", "files");
                config.put("imageMaxSize", 2048000);
                config.put("imageCompressEnable", true);
                config.put("imageCompressBorder", 1600);
                config.put("imageInsertAlign", "none");
                config.put("imageAllowFiles", Arrays.asList(".png", ".jpg", ".jpeg", ".gif", ".bmp"));

                //涂鸦上传配置
                config.put("scrawlActionName", "upload");
                config.put("scrawlUrlPrefix", "http://" + domain + "/upload/");
                config.put("scrawlFieldName", "scrawlImgStr");
                config.put("scrawlMaxSize", 2048000);
                config.put("scrawlInsertAlign", "none");

                //图片列出
                config.put("imageManagerActionName", "listImage");
                config.put("imageManagerListSize", 20);
                config.put("imageManagerUrlPrefix", "http://" + domain + "/upload/");

                return config;
            } else if ("upload".equals(action)) {


                if (!StringUtils.isEmpty(scrawlImgStr)) {
                    //Base64转字节数组
                    byte[] fileByte = Base64Decoder.decode(scrawlImgStr);

                    FileUploadResultVo saveResult = sysFileResourceService.saveFile(fileByte);

                    return saveResult;
                } else {
                    if (files == null || files.length < 1) {
                        return CommonResultVo.fail("file empty");
                    }

                    FileUploadResultVo saveResult = sysFileResourceService.saveFile(files);
                    return saveResult;

                }

            } else if ("listImage".equals(action)) {
                start = (start == null) ? 0 : start;
                Integer size = 20;
                Page<SysFileResource> fileResourcePage = new Page<>(start, size);
                List<SysFileResource> fileResourceList = sysFileResourceService.listFileResource(fileResourcePage);
                List<Map> urls = new ArrayList<>();
                for (SysFileResource resource : fileResourceList) {
                    Map map = new HashMap();
                    map.put("url", resource.getResName());
                    urls.add(map);
                }
                return FileResourcesResultVo.success(urls, start, fileResourcePage.getTotal());
            }


        } catch (Exception e) {
            return CommonResultVo.fail("服务器繁忙，文件上传失败，稍后重试！");
        }

        return null;
    }


    @RequestMapping("/list")
    @ResponseBody
    public Object imagesList(Long start) {

        start = (start == null) ? 0 : start;
        Integer size = 20;
        Page<SysFileResource> fileResourcePage = new Page<>(start, size);
        List<SysFileResource> fileResourceList = sysFileResourceService.listFileResource(fileResourcePage);

        return FileResourcesResultVo.success(fileResourceList, start, fileResourcePage.getTotal());
    }
}
